package com.china08.yxyapi.service.res.huixue.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.service.res.huixue.PlanKnowWeikeService;
import com.china08.yxyapi.service.yxyresource.impl.YxyResourceServiceImpl;

@Service
public class PlanKnowWeikeServiceImpl implements PlanKnowWeikeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PlanKnowWeikeServiceImpl.class);

    @Autowired
    private YxyResourceServiceImpl yxyResourceServiceImpl;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @Override
    public List<YxyResResource> getWeiKeForPlanKnow(List<String> know) {

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // 刨除已删除的
        TermQueryBuilder builderDel = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder.must(builderDel);

        BoolQueryBuilder builderView = new BoolQueryBuilder();
        TermQueryBuilder builderIsYxView = new TermQueryBuilder("isYxView", 1);
        builderView.should(builderIsYxView);

        TermQueryBuilder builderIsYxYunRes = new TermQueryBuilder("isYxYunRes", 1);
        builderView.should(builderIsYxYunRes);
        boolQueryBuilder.must(builderView);

        // 刨除是私有的
        TermQueryBuilder builderViewScop = new TermQueryBuilder("viewScope", 2);
        boolQueryBuilder.must(builderViewScop);

        // 只查询审核已通过的
        TermQueryBuilder builderStatusYes1 = new TermQueryBuilder("status", 1);
        boolQueryBuilder.must(builderStatusYes1);

        // 只查询资源为微课
        TermQueryBuilder buildertTypeId = new TermQueryBuilder("typeId", "yx14");
        boolQueryBuilder.must(buildertTypeId);

        // 只查询上传者ID的
        // TermQueryBuilder builderUploaderId = new
        // TermQueryBuilder("uploaderId", tecId);
        // boolQueryBuilder.must(builderUploaderId);

        // 根据知识点查询(集合)
        BoolQueryBuilder knoBuilder = new BoolQueryBuilder();
        TermsQueryBuilder builderknowOne = new TermsQueryBuilder("knowIds", know);
        TermsQueryBuilder builderknowTwo = new TermsQueryBuilder("knowOneLevelIds", know);
        TermsQueryBuilder builderknowThree = new TermsQueryBuilder("knowTwoLevelIds", know);

        knoBuilder.should(builderknowOne);
        knoBuilder.should(builderknowTwo);
        knoBuilder.should(builderknowThree);
        boolQueryBuilder.must(knoBuilder);

        // vip查询
        BoolQueryBuilder boolQueryBuilderIsvip = new BoolQueryBuilder();
        TermQueryBuilder builderIVIP = new TermQueryBuilder("isVip", true);
        boolQueryBuilderIsvip.must(builderIVIP);
        boolQueryBuilderIsvip.must(boolQueryBuilder);

        // 非vip查询
        BoolQueryBuilder boolQueryBuilderNovip = new BoolQueryBuilder();
        TermQueryBuilder builderNoVIP = new TermQueryBuilder("isVip", false);
        boolQueryBuilderNovip.must(builderNoVIP);
        boolQueryBuilderNovip.must(boolQueryBuilder);

        List<YxyResResource> yrrLst = new ArrayList<>();

        List<ElasticYxyResResource> all = new ArrayList<ElasticYxyResResource>();

        List<ElasticYxyResResource> vipList = new ArrayList<ElasticYxyResResource>();
        List<ElasticYxyResResource> noVipList = new ArrayList<ElasticYxyResResource>();

        vipList = yxyResourceServiceImpl.getListTop(boolQueryBuilderIsvip, 5, "pv");

        if (CollectionUtils.isEmpty(vipList)) {
            vipList = new ArrayList<ElasticYxyResResource>();
        } else {
            all.addAll(vipList);
        }

        if (vipList.size() < 5) {
            noVipList = yxyResourceServiceImpl.getListTop(boolQueryBuilderNovip, (5 - vipList.size()), "pv");
            if (CollectionUtils.isNotEmpty(noVipList)) {
                all.addAll(noVipList);
            }
        }

        if (all.size() > 0) {
            for (ElasticYxyResResource elasticYxyResResource : all) {
                YxyResResource resource = yxyResResourceRepository.findById(elasticYxyResResource.getId());
                yrrLst.add(resource);
            }
        }
        return yrrLst;

    }
}
